﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using EventLogAnalyzer.Common;
using EventLogAnalyzer.Model;

namespace EventLogAnalyzer.ViewModel
{
    internal class MainWindowViewModel : ViewModelBase
    {
        private readonly EventLogAnalyzerModel _model;

        public MainWindowViewModel(EventLogAnalyzerModel model)
        {
            _model = model;
            _model.PropertyChanged += _model_PropertyChanged;
            _model.LogFiles.CollectionChanged += LogFiles_CollectionChanged;
            LogFilesViewModel = new LogFilesViewModel();
            SelectedEventDetails = new EventDetailsViewModel();
            LogDistributionList = new LogDistributionListViewModel();
            LogDistributionList.ShowLogDetailsRequest += LogDistributionList_ShowLogDetailsRequest;
            IsAnalyseButtonEnabled = true;
        }

        void LogDistributionList_ShowLogDetailsRequest(object sender, ShowLogDetailsEventHandlerArgs args)
        {
            LogFilesViewModel.ShowLogs(args.Logs);
        }

        void LogFiles_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                var newItem = (LogFile)e.NewItems[0];
                var logFileViewModel = new LogFileViewModel(newItem);
                logFileViewModel.ShowLogDetailsRequest += new ShowLogDetailsEventHandler(logFileViewModel_ShowLogDetailsRequest);
                logFileViewModel.Logs.CollectionChanged += new NotifyCollectionChangedEventHandler(Logs_CollectionChanged);
                LogFilesViewModel.LogFileViewModels.Add(logFileViewModel);
            }
        }

        void Logs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if(e.Action == NotifyCollectionChangedAction.Add)
            {
                var newItem = (EventLogViewModel)e.NewItems[0];
                LogDistributionList.LogDistributionViewModel[0].AddLog(newItem);
            }
        }

        void logFileViewModel_ShowLogDetailsRequest(object sender, ShowLogDetailsEventHandlerArgs args)
        {
            SelectedEventDetails.EventData = args.Data;
        }


        private LogFilesViewModel _logFilesViewModel;
        public LogFilesViewModel LogFilesViewModel
        {
            get { return _logFilesViewModel; }
            set
            {
                if (_logFilesViewModel != value)
                {
                    _logFilesViewModel = value;
                    OnPropertyChanged("LogFilesViewModel");
                }
            }
        }

        private EventDetailsViewModel _selectedEventDetails;
        public EventDetailsViewModel SelectedEventDetails
        {
            get { return _selectedEventDetails; }
            set
            {
                if (_selectedEventDetails != value)
                {
                    _selectedEventDetails = value;
                    OnPropertyChanged("SelectedEventDetails");
                }
            }
        }

        void _model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
        }

        private RelayCommand _parseEventLogsCommand;
        public RelayCommand ParseEventLogsCommand
        {
            get
            {
                if (_parseEventLogsCommand == null)
                {
                    _parseEventLogsCommand = new RelayCommand(param =>
                                                              {
                                                                  IsAnalyseButtonEnabled = false;
                                                                  ParseFiles(EventLogsFolderPath);
                                                              });
                }
                return _parseEventLogsCommand;

            }
        }

        protected string _eventLogsFolderPath = "E:\\event logs";
        public string EventLogsFolderPath 
        {
            get { return _eventLogsFolderPath; }
            set
            {
                if (_eventLogsFolderPath != value)
                {
                    _eventLogsFolderPath = value;
                    OnPropertyChanged("EventLogsFolderPath");
                }
            }
        }


        private void ParseFiles(string eventLogsFolderPath)
        {
            LogDistributionList.AddLogDistribution(new List<EventLogViewModel>());

            string[] files;
            if (eventLogsFolderPath.EndsWith(".xml"))
                files = new[]{eventLogsFolderPath};
            else
                files = Directory.GetFiles(eventLogsFolderPath);
            _model.ParseFiles(files);
        }

        public LogDistributionListViewModel LogDistributionList { get; set; }

        private bool _isAnalyseButtonEnabled;
        public bool IsAnalyseButtonEnabled
        {
            get { return _isAnalyseButtonEnabled; }
            set
            {
                if (_isAnalyseButtonEnabled != value)
                {
                    _isAnalyseButtonEnabled = value;
                    OnPropertyChanged("IsAnalyseButtonEnabled");
                }
            }
        }


    }
}